*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
 CLASS lcl_fs_interface DEFINITION.

   PUBLIC SECTION.
* Attributes

     DATA iv_json TYPE string.
     DATA ov_json TYPE string.

* Methods
     METHODS get_token
       IMPORTING
                 ls_appid     TYPE t_appid
       RETURNING VALUE(token) TYPE string.

     METHODS get_tasks
       IMPORTING
                 iv_token         TYPE string
                 iv_approval_code TYPE string
       RETURNING VALUE(e_msg)     TYPE string.

     METHODS set_interface_log
       IMPORTING
         comm_scenario  TYPE if_com_management=>ty_cscn_id " string
         service_id     TYPE if_com_management=>ty_cscn_outb_srv_id " string
         comm_system_id TYPE if_com_management=>ty_cscn_id " string
         iv_json        TYPE string
         io_json        TYPE string.

   PROTECTED SECTION.
   PRIVATE SECTION.
     CONSTANTS zfeishu_cs TYPE if_com_management=>ty_cscn_id VALUE 'ZFEISHU_CS' ##NO_TEXT.
     CONSTANTS zfeishu_out_01_rest TYPE if_com_management=>ty_cscn_outb_srv_id VALUE 'ZFEISHU_OUT_01_REST' ##NO_TEXT.
     CONSTANTS zfeishu_out_02_rest TYPE if_com_management=>ty_cscn_outb_srv_id VALUE 'ZFEISHU_OUT_02_REST' ##NO_TEXT.
 ENDCLASS.

 CLASS lcl_fs_interface IMPLEMENTATION.

   METHOD get_token.

     CLEAR: iv_json,ov_json.
     " Content-Type : application/json; charset=utf-8
     DATA lo_http_client  TYPE REF TO if_web_http_client.
     TRY.
         DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*Should be your communication scenario
           comm_scenario = zfeishu_cs
           service_id = zfeishu_out_01_rest
           comm_system_id = CONV #( zfeishu_cs ) ).

         DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).

         DATA(req) = http_client->get_http_request(  ).

         req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
         " req->set_header_field( i_name = 'Accept' i_value = 'application/json' ).

         iv_json = /ui2/cl_json=>serialize( data = ls_appid ).
         req->set_text(  iv_json ).

         DATA(lo_response) = http_client->execute( if_web_http_client=>post ).
         ov_json = lo_response->get_text( ).
         DATA ls_return TYPE  t_return.

         CALL METHOD /ui2/cl_json=>deserialize
           EXPORTING
             json = ov_json
           CHANGING
             data = ls_return.

         IF ls_return-code = 0.
           RETURN ls_return-tenant_access_token.
         ENDIF.

       CATCH cx_root INTO DATA(e).

         DATA(error) = e->get_longtext( ).

     ENDTRY.

     me->set_interface_log( comm_scenario = zfeishu_cs
                            service_id = zfeishu_out_01_rest
                            comm_system_id = zfeishu_cs
                            iv_json = iv_json
                            io_json = ov_json
                            ).
   ENDMETHOD.

   METHOD get_tasks.

     CLEAR: iv_json,ov_json.
     " Content-Type : application/json; charset=utf-8
     DATA lo_http_client  TYPE REF TO if_web_http_client.
     TRY.
         DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*Should be your communication scenario
           comm_scenario = zfeishu_cs
           service_id = zfeishu_out_02_rest
           comm_system_id = CONV #( zfeishu_cs ) ).

         DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).

         DATA(req) = http_client->get_http_request(  ).

         req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
         req->set_form_field( i_name = 'page_size' i_value = '10').
         req->set_authorization_bearer( iv_token ).

         " req->set_header_field( i_name = 'Accept' i_value = 'application/json' ).
         DATA ls_task TYPE t_task.

         ls_task-approval_code = iv_approval_code." |FDCEDE32-BB38-4D8C-B4AE-78067B4C680C|.
         ls_task-locale = |zh-CN|.
         ls_task-order = |2|.
         ls_task-task_start_time_from = |1761965716|.
         ls_task-task_start_time_to = |1764557716|.
         ls_task-task_status = |ALL|.

******************下面大小写转换
         TYPES: BEGIN OF name_mapping,
                  abap TYPE abap_compname,
                  json TYPE string,
                END OF name_mapping.

         DATA: lw_name_mapping TYPE name_mapping,
               name_mappings   TYPE HASHED TABLE OF name_mapping WITH UNIQUE KEY abap.
         DATA: lt_name_mappings    LIKE name_mappings.

         " 大小写mapping，
         lt_name_mappings = VALUE #(
         ( abap = 'APPROVAL_CODE'        json = 'approval_code' )
         ( abap = 'LOCALE' json = 'locale' )
         ( abap = 'ORDER' json = 'order' )
         ( abap = 'TASK_START_TIME_FROM' json = 'task_start_time_from' )
         ( abap = 'TASK_START_TIME_TO' json = 'task_start_time_to' )
         ( abap = 'TASK_STATUS' json = 'task_status' )
         ).

         iv_json = /ui2/cl_json=>serialize(
         data = ls_task
         pretty_name   = /ui2/cl_json=>pretty_mode-low_case " 大写转小写
          name_mappings = lt_name_mappings[]
          ).
         req->set_text(  iv_json ).

         DATA(lo_response) = http_client->execute( if_web_http_client=>post ).
         ov_json = lo_response->get_text( ).
         " TODO 定义结构输出
*         DATA ls_return TYPE  t_return.
*
*         CALL METHOD /ui2/cl_json=>deserialize
*           EXPORTING
*             json = ov_json
*           CHANGING
*             data = ls_return.
*
*         IF ls_return-code = 0.
*           RETURN ls_return-tenant_access_token.
*         ENDIF.

       CATCH cx_root INTO DATA(e).

         DATA(error) = e->get_longtext( ).

     ENDTRY.
     me->set_interface_log( comm_scenario = zfeishu_cs
                                service_id = zfeishu_out_01_rest
                                comm_system_id = zfeishu_cs
                                iv_json = iv_json
                                io_json = ov_json
                                ).
   ENDMETHOD.

   METHOD set_interface_log.

   ENDMETHOD.

 ENDCLASS.
